package com.hongkzh.www.view.fragment;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongkzh.www.other.utils.TitleManager;
import com.hongkzh.www.presenter.BasePresenter;
import com.hongkzh.www.view.iview.BaseIView;

/**
 * mvp framgent 基类
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseIView, View.OnClickListener {

    private View view;
    private P presenter;
    protected TitleManager mTiManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTiManager = new TitleManager(this);
    }

    // 模仿Activity的查找控件
    public View findViewById(int id) {
        return view.findViewById(id);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        setListener();
    }

    protected abstract int getLayoutId();

    protected abstract void init();

    protected abstract void setListener();

    protected void setPresenter(P presenter) {
        this.presenter = presenter;
        presenter.attachView(this);
    }

    public P getPresenter() {
        return presenter;
    }

    @Nullable
    @Override
    public View getView() {
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
}
